package com.xing.crm.service.impl;

import com.xing.crm.mapper.ProRoleMapper;
import com.xing.crm.mapper.ProShopMapper;
import com.xing.crm.mapper.WebUserMapper;
import com.xing.crm.pojo.PageResult;
import com.xing.crm.pojo.ProRole;
import com.xing.crm.pojo.ProShop;
import com.xing.crm.pojo.WebUser;
import com.xing.crm.service.WebUserService;
import com.xing.crm.utils.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.L
 * @version V1.0
 * @date 2021年07月29日 17:01
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class WebUserServiceImpl implements WebUserService {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass().getName());
	@Resource
	private WebUserMapper webUserMapper;
	@Resource
	private ProRoleMapper proRoleMapper;
	@Resource
	private ProShopMapper proShopMapper;
	
	@Override
	public WebUser login(String account, String pwd) {
		return webUserMapper.login(account, MD5Util.stringMD5(pwd));
	}
	
	@Override
	public PageResult<WebUser> list(WebUser webUser, int page, int limit) {
		Map params = new HashMap(0);
		params.put("start", (page - 1) * limit);
		params.put("pageSize", limit);
		params.put("webUser", webUser);
		
		PageResult<WebUser> result = new PageResult();
		result.setCode(0);
		result.setMsg("执行成功");
		result.setCount(webUserMapper.count(params));
		result.setData(webUserMapper.list(params));
		
		return result;
	}
	
	@Override
	public Map<String, Object> edit(WebUser webUser) {
		if(StringUtils.isEmpty(webUser.getName())) {
			webUser.setName(webUser.getAccount());
		}
		String pwd = webUser.getPwd();
		webUser.setPwd(MD5Util.stringMD5(pwd));
		String msg;
		Map<String, Object> result = new HashMap(0);
		result.put("code", 0);
		result.put("msg", "执行成功");
		try {
			if(webUser.getId() == null) {
				if(webUserMapper.checkedAccount(webUser) != null) {
					throw new RuntimeException("该账号已存在");
				}
				int id = webUserMapper.insert(webUser);
				webUser.setId(id);
			} else {
				webUserMapper.edit(webUser);
			}
			msg = "执行成功";
		} catch (Exception e) {
			logger.error("用户编辑操作失败，{}", e.getMessage());
			msg = "RuntimeException".equals(e.getClass().getSimpleName())? e.getMessage() : "操作失败";
			result.put("code", -1);
		}
		result.put("msg", msg);
		return result;
	}
	
	@Override
	public Map<String, Object> updatePwd(WebUser webUser) {
		webUser.setPwd(MD5Util.stringMD5(webUser.getPwd()));
		Map<String, Object> result = new HashMap(0);
		result.put("code", 0);
		result.put("msg", "执行成功");
		result.put("data", webUserMapper.updatePwd(webUser));
		return result;
	}
	
	@Override
	public Map<String, Object> delete(int id) {
		Map<String, Object> result = new HashMap(0);
		result.put("code", 0);
		result.put("msg", "执行成功");
		/* 删除用户角色关联 */
		webUserMapper.delUserRole(id);
		/* 删除商铺关联用户 */
		webUserMapper.delUserShop(id);
		webUserMapper.delete(id);
		return result;
	}
	
	@Override
	public int checkedPwd(WebUser webUser) {
		return webUserMapper.checkedPwd(webUser);
	}
	
	@Override
	public WebUser get(int id) {
		WebUser webUser = null;
		try {
			webUser = webUserMapper.get(id);
		} catch (Exception e) {
		
		}
		return webUser;
	}
	
	@Override
	public Map<String, Object> appointShopManager(Integer sid, Integer uid) {
		Map result = new HashMap(0);
		result.put("code", 0);
		String msg = "重复添加，已拒绝";
		ProRole manager = proRoleMapper.getRoleByName("店长");
		ProShop shop = proShopMapper.getShopUser(uid);
		if(shop == null) {
			if(manager != null) {
				/* 获取店铺店长关联表id */
				List<Map<String, Integer>> shopManager = proShopMapper.getShopManager(manager.getId(), sid);
				if(!shopManager.isEmpty()) {
					Map<String, Integer> item = shopManager.get(0);
					/* 踢除店铺旧店长 */
					proRoleMapper.deleteUserRoleById(item.get("userRole"));
					proShopMapper.delShopUserById(item.get("shopUser"));
				}
				/* 添加用户店铺关联 */
				webUserMapper.appointShopManager(sid, uid);
				/* 添加用户角色关联 */
				webUserMapper.insertUserRole(uid, manager.getId());
				msg = "任命店长成功";
			} else {
				msg = "请先添加角色：店长";
			}
			
		}
		result.put("msg", msg);
		
		return result;
	}
	
	@Override
	public Map<String, Object> getUsers() {
		Map result = new HashMap(0);
		result.put("code", 0);
		result.put("msg", "查询成功");
		List<WebUser> users = webUserMapper.getUsers();
		result.put("data", users);
		return result;
	}
}
